package apis

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"time"
	"vrcm/src/vrcm/app/admin/models/monitor"
	dto "vrcm/src/vrcm/app/admin/service/dto/monitor"
	vrpmDto "vrcm/src/vrcm/app/admin/service/dto/vrpm"
	service "vrcm/src/vrcm/app/admin/service/monitor"
	"vrcm/src/vrcm/common/apis"
)

type MonitorEvent struct {
	apis.Api
}

// GetPage 获取MonitorBasic列表
// @Summary 获取MonitorBasic列表
// @Description 获取MonitorBasic列表
// @Tags MonitorBasic
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.MonitorBasic}} "{"code": 200, "data": [...]}"
// @Router /api/v1/monitor-basic [get]
// @Security Bearer
func (e MonitorEvent) GetPage(c *gin.Context) {
	req := dto.MonitorEventGetPageReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	list := make([]monitor.MonitorEvent, 0)
	var count int64

	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取MonitorEvent失败，失败信息 %s", err.Error()))
		return
	}
	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取MonitorBasic
// @Summary 获取MonitorBasic
// @Description 获取MonitorBasic
// @Tags MonitorBasic
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.MonitorBasic} "{"code": 200, "data": [...]}"
// @Router /api/v1/monitor-basic/{id} [get]
// @Security Bearer
func (e MonitorEvent) Get(c *gin.Context) {
	req := dto.MonitorEventGetReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.Get(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取MonitorEvent失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "查询成功")
}

// Insert 创建MonitorBasic
// @Summary 创建MonitorBasic
// @Description 创建MonitorBasic
// @Tags MonitorBasic
// @Accept application/json
// @Product application/json
// @Param data body dto.MonitorBasicInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/monitor-basic [post]
// @Security Bearer
func (e MonitorEvent) Insert(c *gin.Context) {
	req := dto.MonitorEventInsertReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Insert(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("创建MonitorEvent失败， %s", err.Error()))
		return
	}

	e.OK(req.Id, "创建成功")
}

func (e MonitorEvent) Update(c *gin.Context) {
	req := dto.MonitorEventUpdateReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Update(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("更新MonitorEvent失败， %s", err.Error()))
		return
	}

	e.OK(req.Id, "更新成功")
}

func (e MonitorEvent) Delete(c *gin.Context) {
	req := dto.MonitorEventDeleteReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.Remove(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("删除MonitorEvent失败， %s", err.Error()))
		return
	}
	e.OK(nil, "删除成功")
}

func (e MonitorEvent) SaveProgram(c *gin.Context) {
	req := vrpmDto.SaveProgramReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	errorCode, err := s.SaveProgram(&req)
	if err != nil {
		e.Error(errorCode, err, fmt.Sprintf("保存节目单失败， %s", err.Error()))
		return
	}
	e.OK(nil, "保存节目单成功")
}

func (e MonitorEvent) GetProgramOrigin(c *gin.Context) {
	req := vrpmDto.GetProgramReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetProgramOrigin(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取原始节目单失败， %s", err.Error()))
		return
	}
	e.OK(data, "获取原始节目单成功")
}

func (e MonitorEvent) GetProgram(c *gin.Context) {
	req := vrpmDto.GetProgramReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	res, err := s.GetProgram(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取节目单失败， %s", err.Error()))
		return
	}
	e.OK(res, "获取节目单成功")
}

func (e MonitorEvent) UpdateProgramIndex(c *gin.Context) {
	req := vrpmDto.UpdateProgramIndexReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.UpdateProgramIndex(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("更新节目单失败， %s", err.Error()))
		return
	}
	e.OK(nil, "更新节目单成功")
}

func (e MonitorEvent) SaveProgramRound(c *gin.Context) {
	req := vrpmDto.SaveProgramReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.SaveProgramRound(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("保存节目单失败， %s", err.Error()))
		return
	}
	e.OK(data, "保存节目单成功")
}

func (e MonitorEvent) SaveEventMonitor(c *gin.Context) {
	req := dto.MonitorBasicUpdateReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SaveEventMonitor(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("创建监看失败， %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "创建监看成功")
}

func (e MonitorEvent) RemoveEventMonitor(c *gin.Context) {
	req := dto.EventMonitorDeleteReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.RemoveEventMonitor(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("删除监看失败， %s", err.Error()))
		return
	}
	e.OK(nil, "删除监看成功")
}

func (e MonitorEvent) StartEventMonitor(c *gin.Context) {
	req := dto.StartMonitorEventReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.StartEventMonitor(&req)
	if err != nil {
		if err.Error() == string(apis.EventStartedError.Code) {
			e.Error(apis.EventStartedError.Code, err, fmt.Sprintf("开启活动失败， %s", "活动已开启请先关闭"))
			return
		} else {
			e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("开启活动失败， %s", err.Error()))
		}

		return
	}
	e.OK(nil, "开启活动成功")
}

func (e MonitorEvent) StopEventMonitor(c *gin.Context) {
	req := dto.StopMonitorEventReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.StopEventMonitor(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("关闭活动失败， %s", err.Error()))
		return
	}
	e.OK(nil, "关闭活动成功")
}

func (e MonitorEvent) StartMonitor(c *gin.Context) {
	req := dto.StartMonitorReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.StartMonitor(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("开启监看失败， %s", err.Error()))
		return
	}
	e.OK(nil, "开启监看成功")
}

func (e MonitorEvent) StopMonitor(c *gin.Context) {
	req := dto.StopMonitorReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.StopMonitor(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("关闭监看失败， %s", err.Error()))
		return
	}
	e.OK(nil, "关闭监看成功")
}

func (e MonitorEvent) DeleteMonitor(c *gin.Context) {
	req := dto.DeleteMonitorReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.DeleteMonitor(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("删除监看失败， %s", err.Error()))
		return
	}
	e.OK(nil, "删除监看成功")
}

func (e MonitorEvent) EnableMonitor(c *gin.Context) {
	req := dto.EnableMonitorReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.EnableMonitor(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("开启监看失败， %s", err.Error()))
		return
	}
	e.OK(nil, "开启监看成功")
}

func (e MonitorEvent) SkipProgram(c *gin.Context) {
	req := vrpmDto.SkipProgramReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SkipProgram(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("跳过节目单失败， %s", err.Error()))
		return
	}
	e.OK(nil, "跳过节目单成功")
}

func (e MonitorEvent) GetProgramPage(c *gin.Context) {
	req := dto.ProgramGetPageReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	list := make([]monitor.ProgramRoundModel, 0, 5)
	var count int64
	err = s.GetProgramPage(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取节场记失败， %s", err.Error()))
		return
	}
	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

func (e MonitorEvent) GetProgramByRound(c *gin.Context) {
	req := dto.GetProgramByRoundReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data, err := s.GetProgramByRound(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取节目单失败， %s", err.Error()))
		return
	}
	e.OK(data, "获取节目单成功")
}

func (e MonitorEvent) SaveEventMember(c *gin.Context) {
	req := dto.SaveEventMemberReq{}
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.SaveEventMember(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("保存活动成员失败， %s", err.Error()))
		return
	}
	e.OK(nil, "保存活动成员成功")
}

func (e MonitorEvent) GetEventMember(c *gin.Context) {
	s := service.MonitorEvent{}
	req := dto.GetEventMemberReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	var count int64
	data, err := s.GetEventMember(&req, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取活动成员失败， %s", err.Error()))
		return
	}

	e.PageOK(data, int(count), req.PageIndex, req.PageSize, "获取活动成员成功")
}

func (e MonitorEvent) GetBindEventMember(c *gin.Context) {
	s := service.MonitorEvent{}
	req := dto.GetEventMemberReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetBindEventMember(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取以选中活动成员失败， %s", err.Error()))
		return
	}

	e.OK(data, "获取以选中活动成员成功")
}

func (e MonitorEvent) DeleteEventMember(c *gin.Context) {
	s := service.MonitorEvent{}
	req := dto.DeleteEventMemberReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.DeleteEventMember(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("删除活动成员失败， %s", err.Error()))
		return
	}

	e.OK(nil, "删除活动成员成功")
}

func (e MonitorEvent) GetMemberByCode(c *gin.Context) {
	s := service.MonitorEvent{}
	req := dto.GetMemberByCodeReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetMemberByCode(&req)
	if err != nil {
		if err.Error() == string(apis.WorksiteMemberError.Code) {
			e.Error(apis.WorksiteMemberError.Code, err, fmt.Sprintf("获取成员失败， %s", "成员不存在"))
		} else {
			e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取成员失败， %s", err.Error()))
		}
		return
	}

	e.OK(data, "获取成员成功")
}

func (e MonitorEvent) GetCurrentTime(c *gin.Context) {
	s := service.MonitorEvent{}
	err := e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	now := time.Now().Unix()
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取当前时间失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(now, "获取当前时间成功")
}

func (e MonitorEvent) CopyEventMonitor(c *gin.Context) {
	s := service.MonitorEvent{}
	req := dto.CopyMonitorEventReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.CopyEventMonitor(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("复制活动失败， %s", err.Error()))
		return
	}

	e.OK(nil, "复制活动成功")
}
